package com.neusoft.neuiotms.dcam.workflow.converter;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.neuiotms.dcam.workflow.dto.DiaoboKJDTO;
import com.neusoft.neuiotms.dcam.workflow.model.DiaoboItemKJ;
import com.neusoft.neuiotms.dcam.workflow.model.DiaoboKJ;
import com.neusoft.neuiotms.dcam.workflow.repository.DiaoboItemKJRepository;
import com.neusoft.neuiotms.dcam.workflow.repository.DiaoboKJRepository;

@Component
public class DiaoboKJConverter extends AbstractConverter<DiaoboKJ, DiaoboKJDTO> {

    @Autowired
    private DiaoboKJRepository diaoboKJRepository;
    @Autowired
    private DiaoboItemKJRepository diaoboItemKJRepository;
    @Autowired
    private DiaoboItemKJConverter diaoboItemKJConverter;

    @Override
    public DiaoboKJ toModel(final DiaoboKJDTO dto) {
        DiaoboKJ model;
        if (dto.isNew()) {
            model = new DiaoboKJ();
        } else {
            model = this.diaoboKJRepository.getOne(dto.getId());
        }

        final List<DiaoboItemKJ> list = model.getDetails().stream().collect(Collectors.toList());
        list.forEach(k -> {
            model.getDetails().remove(k);
            this.diaoboItemKJRepository.delete(k);
        });

        dto.getDetails().stream().map(k -> {
            final DiaoboItemKJ detail = this.diaoboItemKJConverter.toModel(k);
            detail.setMain(model);
            return detail;
        }).filter(s -> null != s).forEach(model.getDetails()::add);

        model.setWorkOrderNum(dto.getWorkOrderNum());
        model.setTaskId(dto.getTaskId());
        model.setTitle(dto.getTitle());
        model.setRemark(dto.getRemark());
        model.setCreateUser(dto.getCreateUser());
        model.setCreateUserName(dto.getCreateUserName());
        model.setSrcManagementOrgCode(dto.getSrcManagementOrgCode());
        model.setSrcManagementOrgName(dto.getSrcManagementOrgName());
        model.setDestManagementOrgCode(dto.getDestManagementOrgCode());
        model.setDestManagementOrgName(dto.getDestManagementOrgName());
        model.setReason(dto.getReason());

        return model;
    }

    @Override
    public DiaoboKJDTO toDTO(final DiaoboKJ model, final boolean forListView) {
        final DiaoboKJDTO dto = new DiaoboKJDTO();
        dto.setId(model.getId());
        dto.setWorkOrderNum(model.getWorkOrderNum());
        dto.setTaskId(model.getTaskId());
        dto.setTitle(model.getTitle());
        dto.setRemark(model.getRemark());
        dto.setCreateUser(model.getCreateUser());
        dto.setReason(model.getReason());
        dto.setCreateUserName(model.getCreateUserName());
        dto.setSrcManagementOrgCode(model.getSrcManagementOrgCode());
        dto.setSrcManagementOrgName(model.getSrcManagementOrgName());
        dto.setDestManagementOrgCode(model.getDestManagementOrgCode());
        dto.setDestManagementOrgName(model.getDestManagementOrgName());
        model.getDetails().stream().map(l -> this.diaoboItemKJConverter.toDTO(l))
                .forEach(dto.getDetails()::add);
        return dto;
    }

}
